home *** CD-ROM | disk | FTP | other *** search
/ Champak 132 (Alt) / Vol 132.iso / interfac / it.dig / scripts / __Packages / mx / effects / Tween.as
Encoding:
Text File  |  2011-06-09  |  3.6 KB  |  141 lines

  1. class mx.effects.Tween extends Object
  2. {
  3.    var arrayMode;
  4.    var listener;
  5.    var initVal;
  6.    var endVal;
  7.    var startTime;
  8.    var updateFunc;
  9.    var endFunc;
  10.    var ID;
  11.    static var IntervalToken;
  12.    static var ActiveTweens = new Array();
  13.    static var Interval = 10;
  14.    static var Dispatcher = new Object();
  15.    var duration = 3000;
  16.    function Tween(listenerObj, init, end, dur)
  17.    {
  18.       super();
  19.       if(listenerObj == undefined)
  20.       {
  21.          return undefined;
  22.       }
  23.       if(typeof init != "number")
  24.       {
  25.          this.arrayMode = true;
  26.       }
  27.       this.listener = listenerObj;
  28.       this.initVal = init;
  29.       this.endVal = end;
  30.       if(dur != undefined)
  31.       {
  32.          this.duration = dur;
  33.       }
  34.       this.startTime = getTimer();
  35.       if(this.duration == 0)
  36.       {
  37.          this.endTween();
  38.       }
  39.       else
  40.       {
  41.          mx.effects.Tween.AddTween(this);
  42.       }
  43.    }
  44.    static function AddTween(tween)
  45.    {
  46.       tween.ID = mx.effects.Tween.ActiveTweens.length;
  47.       mx.effects.Tween.ActiveTweens.push(tween);
  48.       if(mx.effects.Tween.IntervalToken == undefined)
  49.       {
  50.          mx.effects.Tween.Dispatcher.DispatchTweens = mx.effects.Tween.DispatchTweens;
  51.          mx.effects.Tween.IntervalToken = setInterval(mx.effects.Tween.Dispatcher,"DispatchTweens",mx.effects.Tween.Interval);
  52.       }
  53.    }
  54.    static function RemoveTweenAt(index)
  55.    {
  56.       var _loc2_ = mx.effects.Tween.ActiveTweens;
  57.       if(index >= _loc2_.length || index < 0 || index == undefined)
  58.       {
  59.          return undefined;
  60.       }
  61.       _loc2_.splice(index,1);
  62.       var _loc4_ = _loc2_.length;
  63.       var _loc1_ = index;
  64.       while(_loc1_ < _loc4_)
  65.       {
  66.          _loc2_[_loc1_].ID--;
  67.          _loc1_ = _loc1_ + 1;
  68.       }
  69.       if(_loc4_ == 0)
  70.       {
  71.          clearInterval(mx.effects.Tween.IntervalToken);
  72.          delete mx.effects.Tween.IntervalToken;
  73.       }
  74.    }
  75.    static function DispatchTweens(Void)
  76.    {
  77.       var _loc2_ = mx.effects.Tween.ActiveTweens;
  78.       var _loc3_ = _loc2_.length;
  79.       var _loc1_ = 0;
  80.       while(_loc1_ < _loc3_)
  81.       {
  82.          _loc2_[_loc1_].doInterval();
  83.          _loc1_ = _loc1_ + 1;
  84.       }
  85.       updateAfterEvent();
  86.    }
  87.    function doInterval()
  88.    {
  89.       var _loc2_ = getTimer() - this.startTime;
  90.       var _loc3_ = this.getCurVal(_loc2_);
  91.       if(_loc2_ >= this.duration)
  92.       {
  93.          this.endTween();
  94.       }
  95.       else if(this.updateFunc != undefined)
  96.       {
  97.          this.listener[this.updateFunc](_loc3_);
  98.       }
  99.       else
  100.       {
  101.          this.listener.onTweenUpdate(_loc3_);
  102.       }
  103.    }
  104.    function getCurVal(curTime)
  105.    {
  106.       if(this.arrayMode)
  107.       {
  108.          var _loc3_ = new Array();
  109.          var _loc2_ = 0;
  110.          while(_loc2_ < this.initVal.length)
  111.          {
  112.             _loc3_[_loc2_] = this.easingEquation(curTime,this.initVal[_loc2_],this.endVal[_loc2_] - this.initVal[_loc2_],this.duration);
  113.             _loc2_ = _loc2_ + 1;
  114.          }
  115.          return _loc3_;
  116.       }
  117.       return this.easingEquation(curTime,this.initVal,this.endVal - this.initVal,this.duration);
  118.    }
  119.    function endTween()
  120.    {
  121.       if(this.endFunc != undefined)
  122.       {
  123.          this.listener[this.endFunc](this.endVal);
  124.       }
  125.       else
  126.       {
  127.          this.listener.onTweenEnd(this.endVal);
  128.       }
  129.       mx.effects.Tween.RemoveTweenAt(this.ID);
  130.    }
  131.    function setTweenHandlers(update, end)
  132.    {
  133.       this.updateFunc = update;
  134.       this.endFunc = end;
  135.    }
  136.    function easingEquation(t, b, c, d)
  137.    {
  138.       return c / 2 * (Math.sin(3.141592653589793 * (t / d - 0.5)) + 1) + b;
  139.    }
  140. }
  141.